package com.project.decoration.controller.appointment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.config.utils.ShiroUtils;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.*;
import com.project.decoration.service.DecorationAppointmentService;
import com.project.decoration.service.DecorationPriceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 预约controller
 * @date 2025/02/13 10:59
 */
@Controller
@ResponseBody
@RequestMapping("appointment")
public class DecorationAppointmentController {

    @Autowired
    private DecorationAppointmentService decorationAppointmentService;
    @Autowired
    private DecorationPriceService decorationPriceService;

    /** 分页获取预约 */
    @PostMapping("getDecorationAppointmentPage")
    public Result getDecorationAppointmentPage(@RequestBody DecorationAppointment decorationAppointment) {
        Page<DecorationAppointment> page = new Page<>(decorationAppointment.getPageNumber(),decorationAppointment.getPageSize());
        QueryWrapper<DecorationAppointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationAppointment.getPriceName()),DecorationAppointment::getPriceName,decorationAppointment.getPriceName())
                .eq(StringUtils.isNotBlank(decorationAppointment.getName()),DecorationAppointment::getName,decorationAppointment.getName())
                .eq(StringUtils.isNotBlank(decorationAppointment.getTel()),DecorationAppointment::getTel,decorationAppointment.getTel())
                .eq(StringUtils.isNotBlank(decorationAppointment.getCity()),DecorationAppointment::getCity,decorationAppointment.getCity())
                .eq(StringUtils.isNotBlank(decorationAppointment.getAddress()),DecorationAppointment::getAddress,decorationAppointment.getAddress())
                .eq(StringUtils.isNotBlank(decorationAppointment.getCommunity()),DecorationAppointment::getCommunity,decorationAppointment.getCommunity())
                .eq(decorationAppointment.getTelTime() != null,DecorationAppointment::getTelTime,decorationAppointment.getTelTime())
                .eq(StringUtils.isNotBlank(decorationAppointment.getCreateBy()),DecorationAppointment::getCreateBy,decorationAppointment.getCreateBy());
        Page<DecorationAppointment> decorationAppointmentPage = decorationAppointmentService.page(page, queryWrapper);
        return Result.success(decorationAppointmentPage);
    }

    /** 根据id获取预约 */
    @GetMapping("getDecorationAppointmentById")
    public Result getDecorationAppointmentById(@RequestParam("id")String id) {
        DecorationAppointment decorationAppointment = decorationAppointmentService.getById(id);
        return Result.success(decorationAppointment);
    }

    @GetMapping("getDecorationAppointmentByUserId")
    public Result getDecorationAppointmentByUserId() {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<DecorationAppointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationAppointment::getUserId,userInfo.getId());
        List<DecorationAppointment> appointmentList = decorationAppointmentService.list(queryWrapper);
        return Result.success(appointmentList);
    }

    /** 保存预约 */
    @PostMapping("saveDecorationAppointment")
    public Result saveDecorationAppointment(@RequestBody DecorationAppointment decorationAppointment) {
        User userInfo = ShiroUtils.getUserInfo();
        decorationAppointment.setUserId(userInfo.getId());
        QueryWrapper<DecorationAppointment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationAppointment::getUserId,userInfo.getId())
                .eq(DecorationAppointment::getTelTime,decorationAppointment.getTelTime())
                .eq(DecorationAppointment::getPriceId,decorationAppointment.getPriceId());
        int count = decorationAppointmentService.count(queryWrapper);
        if (count > 0) {
            return Result.fail("您当日已存在预约记录,不可重复预约");
        }
        DecorationPrice price = decorationPriceService.getById(decorationAppointment.getPriceId());
        decorationAppointment.setPriceName(price.getName());
        boolean save = decorationAppointmentService.save(decorationAppointment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑预约 */
    @PostMapping("editDecorationAppointment")
    public Result editDecorationAppointment(@RequestBody DecorationAppointment decorationAppointment) {
        boolean save = decorationAppointmentService.updateById(decorationAppointment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除预约 */
    @GetMapping("removeDecorationAppointment")
    public Result removeDecorationAppointment(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationAppointmentService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("预约id不能为空！");
        }
    }

}
